package com.prs.crm.dao.sale.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.sale.QuotationSheetDao;
import com.prs.crm.domain.sale.QuotationDetail;
import com.prs.crm.domain.sale.QuotationSheet;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;

public class QuotationSheetDaoImpl extends BaseDaoImpl<QuotationSheet>
		implements QuotationSheetDao {

	@SuppressWarnings("unchecked")
	@Override
	public QuotationDetail getQuotationDetail(final Product product_id,final QuotationSheet quotation_id) {
		return (QuotationDetail)getHibernateTemplate().execute(   
			      new HibernateCallback() {   
			        public Object doInHibernate(Session session) throws HibernateException {   
			        	Criteria criteria =  session.createCriteria(QuotationDetail.class);
			        	criteria.add(Restrictions.eq("product", product_id))
			        		.add(Restrictions.eq("quotationsheet", quotation_id));
			        	List<ProductStore> result = criteria.list();
			        	if (result.size()>0)
			        		return result.get(0);
			        	else
			        		return null;
			        }   
			      }   
			    );  
	}

	@Override
	public QuotationSheet getQuotationSheet(Serializable id) {
		
		return this.getHibernateTemplate().get(QuotationSheet.class, id);
	}

	@Override
	public void saveOrUpdate(QuotationDetail quotationDetail) {
		this.getHibernateTemplate().saveOrUpdate(quotationDetail);
		
	}

	@Override
	public void saveOrUpdateQuotationSheet(QuotationSheet quotationSheet) {
		
		this.getHibernateTemplate().saveOrUpdate(quotationSheet);
		
	}

	

}
